package com.mooke.controller;


import com.mooke.entity.Salary;
import com.mooke.entity.Teacher;
import com.mooke.service.impl.AdminServiceImpl;
import com.mooke.service.impl.TeacherServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping(value = "/teacher")
public class TeacherController {


    @Autowired
    private AdminServiceImpl adminService;
    @Autowired
    private TeacherServiceImpl teacherService;

    // 教师页面显示
    @RequestMapping("/showWages.html")
    public String showWages(HttpServletRequest request, HttpServletResponse response, Model model, Integer page) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String username = request.getParameter("username");
        String teacherID = request.getParameter("username");
        String month = request.getParameter("month");
        model.addAttribute("teacherID", teacherID);
        map.put("teacherID", teacherID);
        if (!"".equals(month) && month != null) {
            map.put("month", month);
            List<Teacher> list = adminService.leave("Admin.leave", map);
            List<Teacher> list2 = adminService.get("Admin.get", map);
            Integer Money = adminService.getCountMoney("Admin.getCountMoney", map);
            Integer Moneyleave=0;
            Integer Moneyget=0;
            if(list.size()!=0){
                Moneyleave  = adminService.getCountMoneyleave("Admin.getCountMoneyleave", map);
            }
            if(list2.size()!=0){
                Moneyget = adminService.getCountMoneyget("Admin.getCountMoneyget", map);
            }
            String state = adminService.getState("Admin.getState", map);
            if ( Moneyleave == null) {
                Moneyleave=0;
            }
            if ( Moneyget == null) {
                Moneyget=0;
            }
            model.addAttribute("Money", Money);
            model.addAttribute("Moneyleave", Moneyleave);
            model.addAttribute("Moneyget", Moneyget);
            model.addAttribute("Salary", Money+Moneyget-Moneyleave);
            model.addAttribute("state", state);
            model.addAttribute("list", list);
            model.addAttribute("list2", list2);
            model.addAttribute("username", username);
            return "teacher/showWages";
        }
        Date day= new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String dateNowStr = sdf.format(day);
        map.put("month", dateNowStr);
        List<Teacher> list = adminService.leave("Admin.leave", map);
        List<Teacher> list2 = adminService.get("Admin.get", map);
        Integer Money = adminService.getCountMoney("Admin.getCountMoney", map);

        Integer Moneyleave=0;
        Integer Moneyget=0;
        if(list.size()!=0){
            Moneyleave  = adminService.getCountMoneyleave("Admin.getCountMoneyleave", map);
        }
        if(list2.size()!=0){
            Moneyget = adminService.getCountMoneyget("Admin.getCountMoneyget", map);
        }
        String state = adminService.getState("Admin.getState", map);
        model.addAttribute("Money", Money);
        model.addAttribute("Moneyleave", Moneyleave);
        model.addAttribute("Moneyget", Moneyget);
        model.addAttribute("Salary", Money+Moneyget-Moneyleave);
        model.addAttribute("state", state);
        model.addAttribute("list", list);
        model.addAttribute("list2", list2);
        model.addAttribute("username", username);
            return "teacher/showWages";
        }


        // 考勤页面显示
        @RequestMapping("/showCheckingin.html")
        public String showCheckingin (HttpServletRequest request, HttpServletResponse response, Model model) throws
        Exception {

            Map<String, Object> map = new HashMap<String, Object>();
            String type = null;
            String findByName = null;
            String username = request.getParameter("username");
            type = request.getParameter("type");
            findByName = request.getParameter("username");
            map.put("type", type);
            map.put("findByName", findByName);
            if ((type == null || "".equals(type)) && findByName != null && !"".equals(findByName)) {
                List<Salary> list = adminService.findCheckinginByName("Admin.findCheckinginByName", map);
                model.addAttribute("list", list);
                model.addAttribute("type", type);
                model.addAttribute("username", username);
                return "teacher/showCheckingin";
            }
            return "teacher/showCheckingin";
        }

        /*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<请假管理>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/

    @RequestMapping("/showVacate.html")
    public String showVacate (HttpServletRequest request, HttpServletResponse response, Model model, Integer page) throws
            Exception {
        Integer curpage = 1;//从第一页开始
        Integer perpage = 10;//每页两条数据
        if (request.getParameter("curpage") != null) {
            curpage = Integer.parseInt(request.getParameter("curpage"));
        }
        ;//此处获取点击的页数

        String findByName = null;
        String username = request.getParameter("username");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("curpage", (curpage - 1) * perpage);
        map.put("perpage", perpage);
            map.put("findByName", username);
            List<Teacher> list = adminService.findByNameVacate1("Admin.findByNameVacate1", map);
            //查询数据库中总条数
            Integer totalcount = adminService.getCountByNameVacate("Admin.getCountVacate", map);
            model.addAttribute("list", list);
            model.addAttribute("username", username);

        Integer totalpage = 0;
        if (totalcount % perpage == 0) {
            totalpage = totalcount / perpage;
        } else {
            totalpage = (totalcount / perpage) + 1;
        }

        model.addAttribute("totalcountStudent", totalcount);
        model.addAttribute("list", list);
        model.addAttribute("curpage", curpage);
        model.addAttribute("totalpage", totalpage);
        model.addAttribute("findByName", findByName);
        model.addAttribute("username", username);
        return "teacher/showVacate";
    }
        @RequestMapping("/toaddVacate.html")
        public String addVacate0 (HttpServletRequest request, HttpServletResponse response, Model model, Integer page) throws
        Exception {
            String username = request.getParameter("username");
            model.addAttribute("username", username);
            return "teacher/addVacate";
        }

         @RequestMapping("/addVacate.html")
         public String addVacate (HttpServletRequest request, HttpServletResponse response, Model model, Integer page) throws
                Exception {
             String teacherID = request.getParameter("teacherID");
             String time = request.getParameter("time");
             String reason = request.getParameter("reason");
             Map<String, Object> map = new HashMap<String, Object>();
             map.put("teacherID", teacherID);
             map.put("time", time);
             map.put("reason", reason);
             teacherService.addVacate("Teacher.addVacate", map);
             model.addAttribute("username", teacherID);
             model.addAttribute("teacherID", teacherID);
            return "teacher/addVacate";
         }

        /*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<奖金管理>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/


        @RequestMapping("/showBonus.html")
        public String showBonus (HttpServletRequest request, HttpServletResponse response, Model model, Integer page) throws
        Exception {


            Integer curpage = 1;//从第一页开始
            Integer perpage = 10;//每页两条数据
            if (request.getParameter("curpage") != null) {
                curpage = Integer.parseInt(request.getParameter("curpage"));
            }
            ;//此处获取点击的页数

            String username = request.getParameter("username");
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("curpage", (curpage - 1) * perpage);
            map.put("perpage", perpage);


                map.put("findByName", username);
                List<Teacher> list = adminService.findByNameBonus("Admin.findByNameBonus", map);
                //查询数据库中总条数
                Integer totalcount = adminService.getCountByNameBonus("Admin.getCountVacate", map);
                model.addAttribute("list", list);
                model.addAttribute("username", username);


            Integer totalpage = 0;
            if (totalcount % perpage == 0) {
                totalpage = totalcount / perpage;
            } else {
                totalpage = (totalcount / perpage) + 1;
            }

            model.addAttribute("totalcountStudent", totalcount);
            model.addAttribute("curpage", curpage);
            model.addAttribute("totalpage", totalpage);

            return "teacher/showBonus";

        }

        /*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<合同管理>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/


        @RequestMapping("/showContract.html")
        public String showContract (HttpServletRequest request, HttpServletResponse response, Model model, Integer page) throws
        Exception {

            String findByName = null;
            String username = request.getParameter("username");
            Map<String, Object> map = new HashMap<String, Object>();
                findByName = request.getParameter("username");
                map.put("findByName", findByName);
                List<Teacher> list = adminService.findByNameTeacher("Admin.findByNameTeacher", map);
                model.addAttribute("list", list);
                model.addAttribute("username", username);
            model.addAttribute("findByName", findByName);

            return "teacher/showContract";

        }
    /*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<个人页面>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
    @RequestMapping("/teacherInformation.html")
    public String teacherInformation (HttpServletRequest request, HttpServletResponse response, Model model, Integer page) throws
            Exception {


        String findByName = request.getParameter("username");
        Map<String, Object> map = new HashMap<String, Object>();
        findByName = request.getParameter("username");
        map.put("findByName", findByName);
        List<Teacher> list = adminService.findByNameTeacher("Admin.findByNameTeacher", map);
        model.addAttribute("list", list);
        model.addAttribute("username", findByName);

        return "teacher/teacherInformation";

    }

    }

